package com.leyou.item.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import com.leyou.item.service.SpecService;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("spec")
public class SpecController {
    @Autowired
    private SpecService specService;
    @RequestMapping("groups/{cid}")
    public ResponseEntity<List<SpecGroup>> queryGroupByCid(@PathVariable("cid") Long cid){
        List<SpecGroup> specGroups = specService.queryGroupByCid(cid);
        if(CollectionUtils.isEmpty(specGroups)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specGroups);
    }
    @PutMapping("group")
    public ResponseEntity<List<SpecGroup>> UpdateGroupByGid(@RequestBody SpecGroup specGroup){
        specService.updateGroupByGid(specGroup);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    @PostMapping("group")
    public ResponseEntity<List<SpecGroup>> insertGroup(@RequestBody SpecGroup specGroup){
        specService.insertGroup(specGroup);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    @GetMapping("params")
    public ResponseEntity<List<SpecParam>> queryParams(
            @RequestParam(value = "gid",required = false) Long gid,
            @RequestParam(value = "cid",required = false) Long cid,
            @RequestParam(value = "generic",required = false) Boolean generic,
            @RequestParam(value = "searching",required = false) Boolean searching){
        List<SpecParam> specParams = specService.queryParams(gid, cid, generic, searching);
        if(CollectionUtils.isEmpty(specParams)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(specParams);
    }
    @DeleteMapping("param/{pid}")
    public ResponseEntity<Void> deleteParam(@PathVariable("pid") Long pid){
        specService.deleteParam(pid);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
    @PostMapping ("/param")
    public ResponseEntity<Void> insertParam(@RequestBody SpecParam specParam){
        specService.insertParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @PutMapping ("/param")
    public ResponseEntity<Void> updateParam(@RequestBody SpecParam specParam){
        specService.updateParam(specParam);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    @DeleteMapping("group/{gid}")
    public ResponseEntity<Void> deleteGroupByGid(@PathVariable("gid") Long gid ){
        specService.deleteGroupByGid(gid);
        return ResponseEntity.status(HttpStatus.OK).build();
    }
}
